<?php
/*
	本插件：$this->model('RSAUtils')->方法名();
	其他地方调用：model('jy_hnapay/RSAUtils')->方法名();
*/
namespace jy_hnapay;
defined('BY_JYA') or exit('error');
class RSAUtils extends \plugin{
	public $pubKey;
    public $privKey;
    public $hnaPubKey;
    function __construct()
    {
        $basic=plugin('jy_hnapay')->plugin_setting('basic');
		if(!$basic['PrivateKey'] || !$basic['PublicKey'] || !$basic['HnapayExpPublicKey']){
			exi('请上传：商户加密私钥,商户加密公钥,快捷类新生公钥！','error');
		}
		//商户加密私钥
        $this->privKey = file_get_contents(ROOT_AT.$basic['PrivateKey']);
        //商户加密公钥
        $this->pubKey = file_get_contents(ROOT_AT.$basic['PublicKey']);
        //快捷类新生公钥
        $this->hnaPubKey = file_get_contents(ROOT_AT.$basic['HnapayExpPublicKey']);
    }

    /**
     * 生成签名
     * @param $signData
     * @return string
     * @throws Exception
     */
    public function sign($signParam)
    {
        $priKey = $this->privKey;
        if (!$priKey) {
            exi("Failed to read private key file！",'error');
        }
        if (!openssl_pkey_get_private($priKey)) {
            exi("Private key is not available",'error');
        }
        $signature = "";
        $res = openssl_get_privatekey($priKey);
        openssl_sign($signParam, $signature, $res, OPENSSL_ALGO_SHA1);
        openssl_free_key($res);
        return base64_encode($signature);
    }

    /**
     * 验证签名：
     * data：原文
     * signature：签名
     * 返回：签名结果，true为验签成功，false为验签失败
     */
    public function verity($signParam, $signature)
    {
        $pubKey = $this->hnaPubKey;
        $res = openssl_get_publickey($pubKey);
        $result = openssl_verify($signParam, base64_decode($signature), $res);
        openssl_free_key($res);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 公钥加密
     * @param $data
     * @return string
     * @throws Exception
     */
    public function encrypt($data)
    {
        $pubKey = $this->hnaPubKey;
        $encrypted = "";
        if (!$pubKey) {
            exi("Failed to read public key file！",'error');
        }
        if (!openssl_pkey_get_public($pubKey)) {
            exi("Public key is not available",'error');
        }


        foreach (str_split($data, 117) as $chunk) {
            openssl_public_encrypt($chunk, $encryptData, $pubKey);
            $encrypted .= $encryptData;
        }
        if (!$encrypted)
            exi('Unable to encrypt data.','error');
        return base64_encode($encrypted);
    }

    /**
     * 私钥解密
     * @param $encryptData
     * @return string
     * @throws Exception
     */
    public function decrypt($encryptData)
    {
        $priKey = $this->privKey;
        if (!$priKey) {
            exi("Failed to read private key file！",'error');
        }
        if (!openssl_pkey_get_private($priKey)) {
            exi("Private key is not available",'error');
        }
        $decrypted = '';
        foreach (str_split(base64_decode($encryptData), 128) as $chunk) {
            openssl_private_decrypt($chunk, $decryptData, $priKey);
            $decrypted .= $decryptData;
        }
        if (!$decrypted)
            exi('Unable to decrypt data.','error');
        return $decrypted;
    }
}
?>